package com.zsix.system.strategy.userByTaskRule;

import com.zsix.system.service.IAssigneeUserService;
import com.zsix.common.entity.taskAssignee.AssigneeUserEntity;
import com.zsix.common.util.RedisUtils;
import com.zsix.common.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 根据开发者参数查找节点处理人,多人处理模式
 */
@Component
public class ByParamsList implements UserByTaskRuleStrategy {
    @Autowired
    private IAssigneeUserService assigneeUserService;
    @Autowired
    private RedisUtils redisUtils;
    public HashMap<String,Object> getUserByTaskRule(String taskid, String ruleName
            , HashMap<String,Object> variables, String procInstId) {
        //获取设置的接收人数据
        HashMap<String, Object> hashMap = assigneeUserService
                .getAssigneeUserWithTaskKey(taskid, ruleName);
        List<AssigneeUserEntity> assigneeUserEntityList = (List<AssigneeUserEntity>) hashMap.get("assigneeUserEntityList");

        String nextWorkEmp = "";
        List<String> byParamsList=new ArrayList<>();
        for (AssigneeUserEntity ae : assigneeUserEntityList) {
            nextWorkEmp+= variables.get(ae.getUsername()).toString()+",";
        }
        if(!StringUtils.isNull(nextWorkEmp)){
            nextWorkEmp = nextWorkEmp.substring(0, nextWorkEmp.length() - 1);
        }
        String[] users=nextWorkEmp.split(",");
        for(int i=0;i<users.length;i++){
            byParamsList.add(users[i].toString());
        }

        //放入缓存，用于发送消息推送
        variables.put(taskid + "_byParamsList", byParamsList);
        if(redisUtils.exists(procInstId+"_sms")){
            redisUtils.remove(procInstId+"_sms");
        }
        redisUtils.set(procInstId+"_sms",nextWorkEmp);
        return variables;
    }
    @Override
    public String getType(){
        return "byParamsList";
    }
}
